<?php
require_once __SITE_PATH.'/Includes/HackAntData.php';
Class AdminController Extends Controller {

	public function index() 
	{
	        $this->hook->template->heading = 'This is the admin Index';
	        
	        $this->hook->template->call('ADMIN');
	}	
	
	private function __getStartWith($letter){		
		$fp = fopen(__SITE_PATH."/Tmp/wordsEn.txt", 'rb');		
		while (!feof($fp)) {
		  $line = fgets($fp);
			if($line[0] === $letter){
				$allListStartWithWord[] = trim($line);
			}
		}
		fclose($fp);
		return $allListStartWithWord;
	}
	
	public function insertData(){
				
		
		$data['range'] = range("A", "Z");
		$data['title'] = 'Inserting the data From ANT.COM.KH';
		/* Mongo conection instance */
		$db = $this->hook->db;
		$collection = $db->english_khmer;		
		if(!empty($_GET['letter'])){
			$letter = trim($_GET['letter']);
			$data['startWithLetter'] = $this->__getStartWith($letter);						
			foreach ($data['startWithLetter']  as $key => $value) {
				$antData = new AntDictionaryGrep($value);
				$objInsert = $antData->getData();
				$collection->insert($objInsert);
				//echo 'Inserted: '.$key. " => ".$value." </br>";
			}
			
			echo 'Done...';			 		
		}
		
		// find everything in the collection
		$data['cursor'] = $collection->find();
		
		$this->hook->template->data = $data;
		$this->hook->template->call('admin');	
		
	}
	public function test(){
		$db = $this->hook->db;
		$collection = $db->english_khmer;
 		//$antData = new AntDictionaryGrep('fuck');
//  		$objInsert = $antData->getData();
//  		$collection->insert($objInsert);
		header('Content-Type: text/html; charset=utf-8');
		$curso = $collection->find(array('word'=>'fuck'));
		foreach($curso as $docs){
			foreach($docs['khmer'] as $khs){
				print_r ($khs['meaning']);
				
			}
			
		}
		
		
	}
} // end of class 
?>
